package com.rabbitmq.rabbitmq.service.impl;

import com.rabbitmq.rabbitmq.config.RpcMessageConfig;
import com.rabbitmq.rabbitmq.service.RpcService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class RpcServiceImpl implements RpcService {

	@Autowired
	RabbitTemplate rabbitTemplate;

	AtomicInteger atomicInteger=new AtomicInteger();

	@Override
	public void serdRpcMessage(Map map) {
		int pre = atomicInteger.incrementAndGet();
		rabbitTemplate.convertAndSend(RpcMessageConfig.rpcExchange,  "RPC-KEY","我是一个rpc消息", new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				MessageProperties messageProperties = message.getMessageProperties();
				messageProperties.setCorrelationId(String.valueOf(pre));
				messageProperties.setReplyTo(RpcMessageConfig.replyQueue);
				System.out.println("......................回调。。。。。。。。。。。。。。"+message);
				return message;
			}

		});
		System.out.println("...........................完结................................");
	}
}
